package health.lm.com.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sunfusheng.marqueeview.MarqueeView;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.List;

import health.lm.com.component_base.arouter.Router;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.widget.CircleImageView.CircleImageView;
import health.lm.com.data.HttpCST;
import health.lm.com.home.R;
import health.lm.com.home.entity.HomeBannerEntity;
import health.lm.com.home.entity.HomeBusinessEntity;
import health.lm.com.home.entity.HomeMultiEntity;
import health.lm.com.home.entity.HomeNoticeMsgEntity;
import health.lm.com.home.entity.HomeRecommendEntity;
import health.lm.com.router.ARouterConstant;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.home.adapter
 * Date：2019/8/17 17:30
 * Introduce：hlian
 */
public class HomeFragmentAdapter extends BaseMultiItemQuickAdapter<HomeMultiEntity, BaseViewHolder> {

    private Banner mBanner;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public HomeFragmentAdapter(List<HomeMultiEntity> data) {
        super(data);
        addItemType(HomeMultiEntity.TYPE_BANNER, R.layout.fragment_home_banner);
        addItemType(HomeMultiEntity.TYPE_NOTICE, R.layout.fragment_home_notice);
        addItemType(HomeMultiEntity.TYPE_BUSINESS, R.layout.fragment_home_business);
        addItemType(HomeMultiEntity.TYPE_RECOMMEND, R.layout.fragment_home_recommend);
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(@NonNull BaseViewHolder helper, HomeMultiEntity item) {
        switch (helper.getItemViewType()) {
            case HomeMultiEntity.TYPE_BANNER:
                mBanner = helper.getView(R.id.banner);
                mBanner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        HomeBannerEntity bannerEntity = (HomeBannerEntity) path;
                        ImageLoaderHelper.getInstance().load(mContext, bannerEntity.getImage(), imageView);
                    }
                });
                mBanner.setImages(item.getBanner());
                mBanner.start();
                //轮播点击事件
                mBanner.setOnBannerListener(position -> {
                    HomeBannerEntity entity = item.getBanner().get(position);
                    if (TextUtils.isEmpty(entity.getUrl())) return;
                    ARouter.getInstance().build(ARouterConstant.JSWebActivity).withString("url", entity.getUrl()).navigation();
                });
                break;
            case HomeMultiEntity.TYPE_NOTICE:
                MarqueeView<HomeNoticeMsgEntity> marqueeView = helper.getView(R.id.marqueeView);
                marqueeView.startWithList(item.getNotices());
                //跳转消息详情
                marqueeView.setOnItemClickListener((position, textView) -> {
                    HomeNoticeMsgEntity msgEntity = item.getNotices().get(position);
                    try {
                        ARouter.getInstance().build(ARouterConstant.NoticesDetailActivity)
                                .withInt(HttpCST._id, Integer.parseInt(msgEntity.getId())).navigation();
                    } catch (NumberFormatException pE) {
                        pE.printStackTrace();
                    }
                });
                break;
            case HomeMultiEntity.TYPE_BUSINESS:
                HomeBusinessEntity businessEntity = item.getBusiness();
                CircleImageView ivAvatar = helper.getView(R.id.imageView9);
                TextView tvName = helper.getView(R.id.textView36);
                TextView tvMemberNum = helper.getView(R.id.tv_member_num);
                TextView tvAddNum = helper.getView(R.id.tv_add_num);
                TextView tvFollow = helper.getView(R.id.tv_followed);
                ImageLoaderHelper.getInstance().load(mContext, businessEntity.getCompany_img(), ivAvatar);
                tvName.setText(businessEntity.getCompany_name());
                tvMemberNum.setText(businessEntity.getMember_num());
                tvAddNum.setText(businessEntity.getMember_add_num());
                tvFollow.setText(businessEntity.getMember_followed());
                break;
            case HomeMultiEntity.TYPE_RECOMMEND:
                RecyclerView recyclerView = helper.getView(R.id.rv_list);
                HomeFragmentRecommendAdapter recommendAdapter = new HomeFragmentRecommendAdapter(item.getRecommend());
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
                recyclerView.setAdapter(recommendAdapter);
                recommendAdapter.setOnItemClickListener((adapter, view, position) -> {
                    HomeRecommendEntity entity = (HomeRecommendEntity) adapter.getData().get(position);
                    ARouter.getInstance().build(ARouterConstant.HLMyCompanyActivity)
                            .withString("id", entity.getId())
                            .withString("title", entity.getName())
                            .navigation();
                });
                break;
        }
    }

    public Banner getBanner() {
        return mBanner;
    }
}
